"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具读取dss文件内容之-p参数验证
Create At   : 2023/4/12
Owner       : opentestcase019
Description :
    1.读取dss文件内容，指定正常-p参数
    2.读取dss文件内容，指定无效-p参数
    3.读取dss文件内容，不指定必选参数
Expect      :
    1.成功
    2.合理报错
    3.合理报错
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.common = Common()

    def test_sharestorage_tool(self):
        step1 = "-----step1:读取dss文件内容，指定正常-p参数;expect:显示成功-----"
        self.logger.info(step1)
        self.logger.info("-----获取卷组名-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Succeed to display lsvg info.", execute_msg,
                      "获取卷组名失败")
        vgname = execute_msg.splitlines()[1].split()[0].strip()
        self.logger.info(vgname)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd examine -p +{vgname}/pg_control -o 0 -f s;" \
                      f"dsscmd examine --path +{vgname}/pg_control -o 0 -f s"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        exp = f"filename is +{vgname}/pg_control, offset is 0."
        self.assertEquals(2, execute_msg.count(exp), "执行失败" + step1)

        step2 = "-----step2:读取dss文件内容，指定无效-p参数;expect:合理报错-----"
        self.logger.info(step2)
        cmd_list = [f'-p {vgname}/pg_control -o 0 -f s',
                    '-p pg_control -o 0 -f s',
                    f'-p +{vgname}/pg_log -o 0 -f s',
                    f'--path {vgname}/pg_control -o 0 -f s',
                    f'--path +{vgname}/pg_log -o 0 -f s']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd examine {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(
                self.primary_node, execute_cmd)
            if cmd == cmd_list[1]:
                self.assertIn("cmd examine error:2193 Path pg_control "
                              "decode error , path should start with +.",
                              execute_msg, "执行失败" + step2)
            elif cmd == cmd_list[2] or cmd == cmd_list[4]:
                self.assertTrue(f"Failed to open dir, path is +{vgname}/pg_log"
                                in execute_msg and
                                f"The file pg_log of +{vgname}/pg_log does not"
                                f" exist" in execute_msg, "执行失败" + step2)
            else:
                self.assertIn(f"cmd examine error:2193 Path "
                              f"{vgname}/pg_control decode error , "
                              f"path should start with +.",
                              execute_msg, "执行失败" + step2)

        step3 = "-----step3:读取dss文件内容，不指定必选参数;expect:合理报错-----"
        self.logger.info(step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd examine"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("args [-p|--path] needs input value.",
                      execute_msg, "执行失败" + step3)

    def tearDown(self):
        self.logger.info("-----无需环境清理-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
